import axios from 'axios'

const service = axios.create({
  timeout: 5000
})

service.interceptors.request.use(config => {
  return config
}, error => {
  return Promise.reject(error)
})

service.interceptors.response.use(response => {
  let data = response.data
  switch (data.status) {
    case 'SUCCESS':
      return data.data
  }
  return Promise.reject(data)
}, error => {
  return Promise.reject(error)
})

export default service

export function fetch (url, params) {
  return new Promise((resolve, reject) => {
    service.get(url, params)
      .then(response => {
        resolve(response)
      })
      .catch((error) => {
        reject(error)
      })
  })
}
